package com.icss.ah.storeApply.action;

import java.util.HashMap;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.LogUtils;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.login.bean.UserInfoBean;

import net.sf.json.JSONObject;
/**
 * 零售店铺认证
 * @author csadchen
 * @date  2017-09-20
 */
@Controller
@Scope("prototype")
public class StoreBindAction extends BaseAction{

	private String shopId;		//店铺id
	private String licenceNo;	//许可证号/非烟零售账号
	private String facilityNum;	//烟草用户简码/非烟零售账号密码
	/**
	 * 进入零售店铺认证页面
	 * @return
	 */
	public String toStoreBindInfoPage(){
		request = this.getRequest();
		try {
			//登录人员信息
			UserInfoBean userInfo = getUserInfoNoSession("");
			//认证零售许可证号/非烟账号
			String licenceNo = userInfo.getLicenceNo();
			//返回认证信息
			HashMap bindInfo = new HashMap();
			//查询参数
			HashMap query = new HashMap();
			
			if(!"".equals(licenceNo)){
				query.put("licenceNo", licenceNo);
				bindInfo = storeBindService.queryStoreBindInfo(query);
			}
			request.setAttribute("userInfo", userInfo);
			request.setAttribute("licenceNo", licenceNo);
			request.setAttribute("bindInfo", bindInfo);
		} catch (Exception e) {
			LogUtils.error(e);
		}
		return "toStoreBindInfoPage";
	}
	/**
	 * 零售认证
	 */
	public void storeBindSave(){
		request = this.getRequest();
		response = this.getResponse();
		jsonText = new Json();
		try {
			HashMap map = new HashMap();
			map.put("shopId", shopId);
			map.put("licenceNo", licenceNo);
			map.put("facilityNum", facilityNum);
			map.put("password", MD5Util.toMD5(facilityNum));
			
			String result = storeBindService.storeBindSave(map);
			String[] str = result.split(",");
			if(str[0].equals("1")){
				jsonText.setSuccess(true);
			} else {
				jsonText.setSuccess(false);
			}
			jsonText.setMsg(str[1]);
			
		} catch (Exception e) {
			LogUtils.error(e);
		} finally{
			JSONObject json = JSONObject.fromObject(jsonText);
			str2Resp(json.toString());
		}
	}
	/**
	 * @return the shopId
	 */
	public String getShopId() {
		return shopId;
	}
	/**
	 * @param shopId the shopId to set
	 */
	public void setShopId(String shopId) {
		this.shopId = shopId;
	}
	/**
	 * @return the licenceNo
	 */
	public String getLicenceNo() {
		return licenceNo;
	}
	/**
	 * @param licenceNo the licenceNo to set
	 */
	public void setLicenceNo(String licenceNo) {
		this.licenceNo = licenceNo;
	}
	/**
	 * @return the facilityNum
	 */
	public String getFacilityNum() {
		return facilityNum;
	}
	/**
	 * @param facilityNum the facilityNum to set
	 */
	public void setFacilityNum(String facilityNum) {
		this.facilityNum = facilityNum;
	}
}
